using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

using Common;
public partial class controls_tm_GroupList : System.Web.UI.UserControl
{
    //default height and width value of treeview
    private int myHeight = 200;
    private int myWidth = 200;

    #region Property
    //global treeview
    public TreeView myTreeView
    {
        get { return this.TreeViewObj; }
    }
    public int MyHeight
    {
        set { myHeight = value; }
        get { return myHeight; }
    }
    public int MyWidth
    {
        set { myWidth = value; }
        get { return myWidth; }
    }
    #endregion Property

    #region protected function(Page_Load,PopulateNodes,PopulateRootLevel)
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            //show tree on page load
            PopulateRootLevel();

            //set Width and Height panel
            TreeViewObj.Width = myWidth;
            TreeViewObj.Height = MyHeight;
        }
    }
    protected void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
    {
        foreach (DataRow dr in dt.Rows)
        {
            TreeNode tn = new TreeNode();
            tn.Text = "<strong>" + dr["groupCode"].ToString().Trim() + "</strong>";
            tn.Value = dr["groupId"].ToString().Trim();
            tn.SelectAction = TreeNodeSelectAction.None;
            nodes.Add(tn);
        }
    }
    protected void PopulateRootLevel()
    {
        string SQLCmd = "SELECT groupId,groupCode FROM tm_user_groups";
        
        SqlConnection connection = new SqlConnection(sysLibs.GetConnectionString);
        SqlCommand command = new SqlCommand(SQLCmd, connection);
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        DataTable dt = new DataTable();
        adapter.Fill(dt);

        PopulateNodes(dt, myTreeView.Nodes);
    }
    #endregion protected function(Page_Load,PopulateNodes,PopulateRootLevel)

    #region public function(GetSelectedNodesArray,GetSelectedNodesHashtable)
    /// <summary>
    /// get all selected node in tree, value return is ArrayList
    /// </summary>
    /// <returns>return ArrayList</returns>
    public ArrayList GetSelectedNodesArray()
    {
        ArrayList seletedNodes = new ArrayList();

        if (myTreeView.CheckedNodes.Count > 0)
        {
            foreach (TreeNode tn in myTreeView.CheckedNodes)
            {
                seletedNodes.Add(tn.Value.Trim());
            }
        }
        return seletedNodes;
    }
    
    /// <summary>
    /// get all selected node in tree, value return is HashTable
    /// </summary>
    /// <returns>return HashTable</returns>
    public Hashtable GetSelectedNodesHashtable()
    {
        Hashtable seletedNodes = new Hashtable();

        if (myTreeView.CheckedNodes.Count > 0)
        {
            foreach (TreeNode tn in myTreeView.CheckedNodes)
            {
                seletedNodes.Add(tn.Text.Trim(), tn.Value.Trim());
            }
        }
        return seletedNodes;
    }
    #endregion public function(GetSelectedNodesArray,GetSelectedNodesHashtable)
}
